package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.settings.dto.UserLoginDto;
import com.bjpowernode.crm.settings.service.UserService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 *
 * @author 刘逸晖
 */
@Getter
@Setter
@RequestMapping
public class UserController {

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    /**
     * 跳转至登录页
     *
     * @return 登录页
     */
    @GetMapping("/settings/qx/user/toLogin.do")
    public String toLogin() {
        return "settings/qx/user/login";
    }

    /**
     * 登录
     *
     * @param userLoginDto 用户名、密码等
     * @param request
     * @param response
     * @param session
     * @return 是否登录成功
     */
    @PostMapping("/settings/qx/user/login.do")
    @ResponseBody
    public ReturnObject login(UserLoginDto userLoginDto, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        return this.userService.login(userLoginDto, request, response, session);
    }

    /**
     * 退出登录
     *
     * @param response
     * @param session
     * @return 首页地址
     */
    @GetMapping("/settings/qx/user/logout.do")
    public String logout(HttpServletResponse response, HttpSession session) {
        this.userService.logout(response, session);
        return "redirect:/";
    }

}
